package com.chb.service.impl;

import com.chb.dao.BranchGoodsDao;
import com.chb.dao.GoodsDao;
import com.chb.dao.MainDao;
import com.chb.dao.StaffDao;
import com.chb.model.entity.BranchGoods;
import com.chb.model.entity.Department;
import com.chb.model.entity.Goods;
import com.chb.model.entity.Staff;
import com.chb.model.vo.Result;
import com.chb.model.vo.StaffAndDepartmentVo;
import com.chb.model.vo.StaffVo;
import com.chb.service.MainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: 陈海彬
 * @date: Created in 2023/10/8 11:57
 * @description:
 * @modified By:
 * @version:
 */
@Service
public class MainServiceImpl implements MainService {
    @Autowired
    private HttpSession session;
    @Autowired
    private MainDao mainDao;
    @Autowired
    private GoodsDao goodsDao;
    @Autowired
    private StaffDao staffDao;
    @Autowired
    private BranchGoodsDao branchGoodsDao;

    /**
     * @create by: 陈海彬
     * @description: 获取主页需要的数据
     * @create time: 2023/10/8 11:59
     */
    @Override
    public Result getAllInfo() {
        Map<String, Object> map = new HashMap<>();
        Staff staff = (Staff) session.getAttribute("staff"); //  获取session

        // 获取用户名称、所属分店名称
        StaffVo staffVo = mainDao.getStaffNameAndBranchNameByStaffId(staff.getStaffid());

        //  获取用户信息
        Map<String, Object> branchInfo = new HashMap<>();
        //  获取店铺总数
        Integer branchCount = mainDao.getBranchCount();
        branchInfo.put("branchCount", branchCount);
        //  获取员工总数
        Integer staffCount = mainDao.getStaffCountByBranchId(staff.getBranchid());
        branchInfo.put("staffCount", staffCount);
        //  获取商品总数
        Integer goodsCount = mainDao.getGoodsCount(staff.getBranchid());
        branchInfo.put("goodsCount", goodsCount);

        //  商品售价信息
        List<Goods> allGoods = goodsDao.getAllGoods();
        //  部门员工统计
        List<StaffAndDepartmentVo> departmentCountList = staffDao.findStaffByBranchIdAndState(staff.getBranchid());
        List<Department> allDepartment = mainDao.getAllDepartment();
        List allStaff = new ArrayList<>();
        for (Department department : allDepartment) {
            Map<String, Object> deptMap = new HashMap<>();
            deptMap.put("deptId", department.getDeptid());
            deptMap.put("name", department.getDeptname());
            for (StaffAndDepartmentVo s : departmentCountList) {
                Integer deptid = s.getDeptid();
                if (department.getDeptid().equals(deptid)) {
                    deptMap.put("value", s.getCount());
                    break;
                } else {
                    deptMap.put("value", 0);
                }
            }
            allStaff.add(deptMap);
        }

        //  商品库存统计
        List<BranchGoods> branchGoodsList = branchGoodsDao.getBranchGoodsByBranchId(staff.getBranchid());
        Map<String, Object> branchMap = new HashMap<>();
        List countList = new ArrayList<>();
        List goodsnameList = new ArrayList<>();
        for (BranchGoods branchGoods : branchGoodsList) {
            Integer counts = branchGoods.getCounts();
            countList.add(counts);
            String goodsname = branchGoods.getGoods().getGoodsname();
            goodsnameList.add(goodsname);
        }
        branchMap.put("countList", countList);
        branchMap.put("goodsnameList", goodsnameList);


        map.put("staff", staffVo);
        map.put("branchInfo", branchInfo);
        map.put("allGoods", allGoods);
        map.put("allStaff", allStaff);
        map.put("branchGoodsInfo", branchMap);
        return Result.success(map);
    }
}